// 1.导入
import {
    createRouter,
    createWebHashHistory
} from 'vue-router'


const routes = [{
        path: '/',
        name: 'layout',
        component: () => import('../views/layout/index.vue'),
        //路由重定向
        redirect: "/home",
        children: [{
                name: 'home',
                path: 'home',
                component: () => import('@/views/home')
            },
            {
                name: 'buy',
                path: 'buy',
                component: () => import('../views/buy')
            }
        ]

    },
    {
        path: '/',
        name: 'loginOrRegister',
        component: () => import('../views/login_register/index.vue'),
        // path: "/:pathMatch(.*)*",
        redirect: "/login",
        children: [{
                path: 'login',
                name: 'login',
                component: () => import('../views/login'),
                beforeEnter: (to, from, next) => {
                    if (from.path === '/shoppingCart') {
                        next('/home')
                    } else {
                        next()
                    }

                }
            },
            {
                path: 'register',
                name: 'register',
                component: () => import('../views/register')
            }
        ]
    },
    {
        name: 'shoppingCart',
        path: '/shoppingCart',
        component: () => import('../views/shoppingcart')
    }

]
const router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior() {
        return {
            top: 0
        }
    }
})

// 导出路由
export default router